import {
	uploadUrl
} from '@/pages/api/report/helperOrderp.js'
import {
	getByOrdersId,
	updatechutu,
	orderhandle
} from '@/pages/api/report/orderp.js'
import {
	getOrderDetail,
	getTakeTask,
	postOrderHandle,
	orgByNodeId,
	allocate,
	allocatedo,
	postWithdraw,
	postOrderAudit,
	postCheck,
	getMyList,
	postCloseOrder,
	postBackStep,
	cancelTask,
	allocatepostWithdraw,
	allocatepostCloseOrder,
	getPersonListApi,
	appointTaskApi
} from '@/pages/api/orderp/orderp.js'
import {
	mapState
} from 'vuex'
export default {
	computed: {
		...mapState(['userInfo', 'path']),
		inputed() {
			if (this.checkop == 1) {
				return false
			} else {
				return true
			}
		}
	},
	methods: {
		//转发
		forward() {
			this.personShow = true
		},
		//指派工单
		appointTask(val) {
			console.log(val)
			let uid = ''
			this.personList.forEach((item, index) => {
				uid = this.personList[val[0]].uid
			})
			appointTaskApi({
				id: this.id,
				uid: uid
			}).then((res) => {
				console.log(res)
				uni.$emit('updatelist', 10)
				this.$refs.uToast.show({
					title: '转发成功',
					type: 'success',
					url: '/pages/index/approval/index'
				}, 500)
			})
		},
		//预览图片
		previewPic(item) {
			console.log("图片操作事件", item)
			let arr = []
			arr.push(item)
			console.log('图片预览', arr)
			uni.previewImage({
				urls: arr
			})
		},
		// 获取人员列表
		getPersonList() {
			getPersonListApi({
				id: this.id
			}).then((res) => {
				console.log(res)
				this.personList = res[0].persons
			})
		},
		changesw(e) {
			this.scrollTop = this.old.scrollTop
			this.$nextTick(() => {
				this.scrollTop = 0
			});
			this.tabCurrent = e.detail.current
		},
		//更新图片地址
		// updateUrlList(list,type) {
		// 	console.log('list',list,type)
		// 	let formList = ['chukou','chongxi','fangchen','dibang','shipin','qita']
		// 	this.form[formList[type]] = list
		// 	console.log(this.form)
		// },
		upchange(a, b, c) {
			console.log('change错误', a, b, c)
		},
		uppross(a, b, c) {
			console.log('pross错误', a, b, c)
		},
		uperror(a, b, c) {
			console.log('上传错误', a, b, c)
		},
		//分派关闭工单
		allocateout() {
			if (this.outorder != 0) {
				uni.showModal({
					title: '提示',
					content: '是否确定提交？',
					confirmText: '确定',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							if (this.outorder == 1) {
								allocatepostCloseOrder({
									id: this.id,
									other: {
										result: "关闭",
										content: this.type_value
									}
								}).then(d => {
									uni.$emit('updatelist', 10)
									this.$refs.uToast.show({
										title: '关闭成功',
										type: 'success',
										url: '/pages/index/approval/index'
									}, 500)
								})
							} else if (this.outorder == 2) {
								allocatepostWithdraw({
									id: this.id,
									other: {
										result: "退单整改",
										content: this.type_value
									}
								}).then(res => {
									uni.$emit('updatelist', 10)
									this.$refs.uToast.show({
										title: '退单成功',
										type: 'success',
										url: '/pages/index/approval/index'
									}, 500)
									// uni.navigateBack()
								})
							} else {
								postBackStep({
									id: this.id,
									other: {
										result: "返回上一节点",
										content: this.type_value
									}
								}).then(res => {
									uni.$emit('updatelist', 10)
									this.$refs.uToast.show({
										title: '返回上一节点成功',
										type: 'success',
										url: '/pages/index/approval/index'
									}, 500)
								})
							}

						}
					}
				});
			}
		},
		tabChange(index) {
			this.scrollTop = this.old.scrollTop
			this.$nextTick(() => {
				this.scrollTop = 0
			});
			this.tabCurrent = index
		},
		//取消认领
		canceltake() {
			uni.showModal({
				title: '提示',
				content: '是否确定取消认领？',
				confirmText: '确定',
				cancelText: '取消',
				success: res => {
					if (res.confirm) {
						cancelTask({
							id: this.id
						}).then(res => {
							this.orderType = 0
							uni.$emit('update_workorder',this.id )
						})
					}
				}
			})
		},
		//更新申报表
		updatechutumsg() {
			let params = {
				accessRoadSclerosis: this.form.chukou.join(','),
				flushingEquipment: this.form.chongxi.join(','),
				dustControl: this.form.fangchen.join(','),
				videoSurveillance: this.form.shipin.join(','),
				weighbridge: this.form.dibang.join(','),
				other: this.form.qita.join(','),
				loessEarthQuantity: this.detailmsg.loessEarthQuantity1 + ',' + this.detailmsg.loessEarthQuantity2,
				plantingSoilEarthQuantity: this.detailmsg.plantingSoilEarthQuantity1 + ',' + this.detailmsg
					.plantingSoilEarthQuantity2,
				muckEarthQuantity: this.detailmsg.muckEarthQuantity1 + ',' + this.detailmsg.muckEarthQuantity2,
				slurryEarthQuantity: this.detailmsg.slurryEarthQuantity1 + ',' + this.detailmsg
					.slurryEarthQuantity2,
				wasterEarthQuantity: this.detailmsg.wasterEarthQuantity1 + ',' + this.detailmsg
					.wasterEarthQuantity2,
				otherEarthQuantity: this.detailmsg.otherEarthQuantity1 + ',' + this.detailmsg.otherEarthQuantity2,
				allEarthQuantity1: this.detailmsg.allEarthQuantity1 + ',' + this.detailmsg.allEarthQuantity2,
				endTime: this.time1,
				startTime: this.time,
				id: this.detailmsg.id,
				isPromise: 1,
				fourArea: this.$store.state.path.toString(),
				siteName: this.detailmsg.siteName
			}
			updatechutu(params).then(res => {
				console.log(res)
				this.$refs.uToast.show({
					title: '提交成功',
					type: 'success'
				}, 300)
			}).catch(e => {
				// console.log(e.response.data.msg)
			})


		},
		//通过审核跳转
		passin() {
			// console.log(this.checkop, 77777)
			if (this.checkop == 2) {
				// console.log("审核跳转" + this.checkop, 77777)
				uni.showModal({
					title: '提示',
					content: '是否确定提交审核？',
					confirmText: '确定',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							postCheck({
								id: this.id,
								other: {
									result: "工单核验",
									content: '4'
								}
							}).then(res => {
								this.show = false
								this.$refs.uToast.show({
									title: '处置成功',
									type: 'success'
								}, 300)
								// uni.$emit('updatelist', 10)
								// uni.navigateTo({
								// 	url: '/pages/index/approval/chutu/passin'
								// })
								uni.navigateTo({
									url: '/pages/index/listCard/home'
								})
								console.log(res)
							})
						}
					}
				});
			} else {
				if (!this.redshow1 && !this.redshow2 && !this.redshow3 && !this.redshow4 && !this.redshow5 && !this
					.redshow6) {
					console.log('总量', this.detailmsg.allEarthQuantity)
					if (this.detailmsg.allEarthQuantity1 >= 0 && this.detailmsg.allEarthQuantity2 >= 0) {
						uni.showModal({
							title: '提示',
							content: '是否确定提交审核？',
							confirmText: '确定',
							cancelText: '取消',
							success: res => {
								if (res.confirm) {
									postCheck({
										id: this.id,
										other: {
											result: "工单核验",
											content: '2'
										}
									}).then(res => {
										this.getorgByNodeId()
										this.show = false
										this.buttontype = 10
										this.updatechutumsg()

										setTimeout(() => {
											getMyList().then(res => {
												res.rows.forEach(d => {
													if (d.id == this.idd) {
														this.id = d.recordId
													}
												})
											})
										}, 300)

									})
								}
							}
						});
					} else {
						this.$refs.uToast.show({
							title: '请完整填写出土量',
							type: 'warning'
						}, 300)
					}

				} else {
					this.$refs.uToast.show({
						title: '请正确填写出土量或框选四至范围',
						type: 'warning'
					}, 300)
				}
			}

		},
		//获取分配部门
		getorgByNodeId() {
			orgByNodeId({
				nodeId: this.nodeId
			}).then(res => {
				console.log(res, '----------------res')
				res.forEach(d => {
					this.list.push({
						nodeId: d.nodeId,
						orgId: d.orgId,
						orgName: d.orgName,
						checked: false,
						disabled: false
					})
				})
				console.log(res, 9999999, this.nodeId)
			})
		},
		getolddetail() {
			return new Promise((resolve, reject) => {
				getByOrdersId({
					ordersId: this.idd
				}).then(res => {
					let str = '350821 1998041349 17'
					console.log(res, 333)
					this.detailmsg = res
					if (res) {
						//身份证打码  
						this.detailmsg.reportPersonIdcard = this.detailmsg.reportPersonIdcard.slice(0,
							6) + '**********' + this.detailmsg.reportPersonIdcard.slice(16, 18)
						this.detailmsg.loessEarthQuantity1 = this.detailmsg.loessEarthQuantity.split(
							',')[0]
						this.detailmsg.loessEarthQuantity2 = this.detailmsg.loessEarthQuantity.split(
							',')[1]
						this.detailmsg.plantingSoilEarthQuantity1 = this.detailmsg
							.plantingSoilEarthQuantity.split(',')[0]
						this.detailmsg.plantingSoilEarthQuantity2 = this.detailmsg
							.plantingSoilEarthQuantity.split(',')[1]
						this.detailmsg.muckEarthQuantity1 = this.detailmsg.muckEarthQuantity.split(',')[
							0]
						this.detailmsg.muckEarthQuantity2 = this.detailmsg.muckEarthQuantity.split(',')[
							1]
						this.detailmsg.slurryEarthQuantity1 = this.detailmsg.slurryEarthQuantity.split(
							',')[0]
						this.detailmsg.slurryEarthQuantity2 = this.detailmsg.slurryEarthQuantity.split(
							',')[1]
						this.detailmsg.wasterEarthQuantity1 = this.detailmsg.wasterEarthQuantity.split(
							',')[0]
						this.detailmsg.wasterEarthQuantity2 = this.detailmsg.wasterEarthQuantity.split(
							',')[1]
						this.detailmsg.otherEarthQuantity1 = this.detailmsg.otherEarthQuantity.split(
							',')[0]
						this.detailmsg.otherEarthQuantity2 = this.detailmsg.otherEarthQuantity.split(
							',')[1]
						this.detailmsg.allEarthQuantity1 = this.detailmsg.allEarthQuantity.split(',')[0]
						this.detailmsg.allEarthQuantity2 = this.detailmsg.allEarthQuantity.split(',')[1]
						this.detailmsg.dangEarthQuantity1 = this.detailmsg.dangEarthQuantity.split(',')[
							0]
						this.detailmsg.dangEarthQuantity2 = this.detailmsg.dangEarthQuantity.split(',')[
							1]
						this.time = this.detailmsg.startTime
						this.time1 = this.detailmsg.endTime
						console.log(this.detailmsg.accessRoadSclerosis)

						this.detailmsg.tenderList = this.detailmsg.tenderList.split(',') || ''
						this.detailmsg.ownerCompany.businessLicense = this.detailmsg.ownerCompany
							.businessLicense.split(',') || ''
						this.detailmsg.constructionCompany.businessLicense = this.detailmsg
							.constructionCompany.businessLicense.split(',')
						this.detailmsg.buildCompany.businessLicense = this.detailmsg
							.buildCompany.businessLicense.split(',')
						//出口道路硬化
						if (this.detailmsg.accessRoadSclerosis == null || this.detailmsg
							.accessRoadSclerosis == '') {
							this.detailmsg.accessRoadSclerosis = []
							this.form.chukou = []
						} else {
							this.form.chukou = this.detailmsg.accessRoadSclerosis.split(',')
							this.detailmsg.accessRoadSclerosis = this.detailmsg.accessRoadSclerosis
								.split(',')
						}
						//冲洗设备
						if (this.detailmsg.flushingEquipment == null || this.detailmsg
							.flushingEquipment == '') {
							this.form.chongxi = []
							this.detailmsg.flushingEquipment = []
						} else {
							this.form.chongxi = this.detailmsg.flushingEquipment.split(',')
							this.detailmsg.flushingEquipment = this.detailmsg.flushingEquipment.split(
								',')
						}
						//防尘降尘
						if (this.detailmsg.dustControl == null || this.detailmsg.dustControl == '') {
							this.form.fangchen = []
							this.detailmsg.dustControl = []
						} else {
							this.form.fangchen = this.detailmsg.dustControl.split(',')
							this.detailmsg.dustControl = this.detailmsg.dustControl.split(',')
						}
						//地磅
						if (this.detailmsg.weighbridge == null || this.detailmsg.weighbridge == '') {
							this.detailmsg.weighbridge = []
							this.form.dibang = []
						} else {
							this.form.dibang = this.detailmsg.weighbridge.split(',')
							this.detailmsg.weighbridge = this.detailmsg.weighbridge.split(',')
						}
						//视频
						if (this.detailmsg.videoSurveillance == null || this.detailmsg
							.videoSurveillance == '') {
							this.detailmsg.videoSurveillance = []
							this.form.shipin = []
						} else {
							this.form.shipin = this.detailmsg.videoSurveillance.split(',')
							this.detailmsg.videoSurveillance = this.detailmsg.videoSurveillance.split(
								',')
						}
						//其他
						if (this.detailmsg.other == null || this.detailmsg.other == '') {
							this.detailmsg.other = []
							this.form.qita = []
						} else {
							this.form.qita = this.detailmsg.other.split(
								',') //此处对form图片做数据回填--未操作图片也可直接提交到下一流程
							this.detailmsg.other = this.detailmsg.other.split(',')

							console.log(this.detailmsg.other)
						}
						// 图片显示操作
						this.nameList.forEach((item, index) => {
							console.log(this.detailmsg[item.id])
							item.picList = this.detailmsg[item.id]
						})

						console.log('222222222出土照片', this.detailmsg)


						console.log('this.nameList', this.nameList)
						this.getphotolist = this.detailmsg.accessRoadSclerosis.concat(this.detailmsg
							.tenderList, this.detailmsg
							.flushingEquipment, this.detailmsg.dustControl, this.detailmsg
							.weighbridge, this.detailmsg.videoSurveillance, this.detailmsg.other)
					}

					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		getdetail() {
			return new Promise((resolve, reject) => {
				getOrderDetail({
					id: this.id
				}).then(res => {
					console.log(res)
					this.detailList = res
					this.orderType = this.detailList.orderType
					this.buttontype = this.detailList.type
					this.detailList.formValue.forEach(d => {
						if (d.display_name == '类型') {
							this.yjtype = d[d.name]
						}
						if (d.display_name == '车牌') {
							this.getcarnumber = d[d.name]
						}
						if (d.display_name == '时间') {
							this.yjtime = d[d.name]
						}
						if (d.display_name == '位置') {
							this.location = d[d.name]
							this.test.getRegeo({
								location:   parseFloat(this.location[0]) + ',' + parseFloat(this.location[1]) , //location的格式为'经度,纬度'
								success: (data)=> {
									console.log('地点',data[0].regeocodeData.formatted_address)
									this.address = data[0].regeocodeData.formatted_address
								}
							})
						}
						if (d.handleTime && d.occurenceLocation) {
							this.occurenceLocation = d.occurenceLocation
							this.pdtime = d.handleTime
						}
					})
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		//确认审核
		comfirm() {
			console.log(this.pass_value,this.chuzhilist)
			if (this.buttontype == 2) {
				uni.showModal({
					title: '提示',
					content: '是否确定提交处置？',
					confirmText: '确定',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							postOrderHandle({
								id: this.id,
								other: {
									result: "工单处置",
									content: this.pass_value,
									pic: this.chuzhilist.join(',')
								}
							}).then(res => {
								this.$refs.uToast.show({
									title: '处置成功',
									type: 'success',
									url: '/pages/index/listCard/home'
								}, 300)
								// uni.navigateBack()
							})
						}
					}
				});
			}else{
				uni.showModal({
					title: '提示',
					content: '是否通过审核？',
					confirmText: '确定',
					cancelText: '取消',
					success: res => {
						if (res.confirm) {
							allocatedo({
									id: this.id,
									formValue: [{
										name: "orderName",
										type: "text",
										params: {},
										display_name: "分派处置内容",
										orderName: this.type_value,
									}, ],
									other: {
										result: "部门审核通过",
										content: this.pass_value,
										pic: this.chuzhilist.join(',')
									}
								}).then(res => {
								this.show = false
								let params = {
									id: this.id,
									formValue: [{
										name: "orderName",
										type: "text",
										params: {},
										display_name: "分派处置内容",
										orderName: this.type_value,
									}, ],
									other: {
										result: "部门审核通过",
										content: this.pass_value,
										pic: this.chuzhilist.join(','),
										orderId:this.idd
									}
								}
								console.log(JSON.stringify(params))
								orderhandle(params).then(d=>{
									this.$refs.uToast.show({
										title: '处置成功',
										type: 'success',
										url: '/pages/index/listCard/home'
									}, 300)
									// console.log(d)
								}).finally(f=>{
									this.$refs.uToast.show({
										title: '处置成功',
										type: 'success',
										url: '/pages/index/listCard/home'
									}, 300)
								})
								
							})
						}
					}
				});
				
			}
			
		},
		//任务认领
		taketask() {
			uni.showModal({
				title: '提示',
				content: '是否确定认领？',
				confirmText: '确定',
				cancelText: '取消',
				success: res => {
					if (res.confirm) {
						getTakeTask({
							id: this.id
						}).then(res => {
							this.orderType = 1
							console.log(res)
						})
					}
				}
			})

		},

		// 选中某个复选框时，由checkbox时触发
		checkboxChange(e) {
			console.log(e);
			if (e.value) {
				this.activelist.push(e.name)
			} else {
				this.activelist.pop()
			}
		},
		// 全选
		checkedAll() {
			this.list.map(val => {
				val.checked = true;
			})
		},
		//退单
		passout(index) {
			uni.showModal({
				title: '提示',
				content: '是否确定退单整改？',
				confirmText: '确定',
				cancelText: '取消',
				success: res => {
					if (res.confirm) {
							postBackStep({
								id: this.id,
								other: {
									result: "返回上一节点",
									content: this.type_value
								}
							}).then(res => {
								uni.$emit('updatelist', 10)
								this.$refs.uToast.show({
									title: '退单成功',
									type: 'success',
									url: '/pages/index/listCard/home'
								}, 500)
							}).catch(res => {
								console.log(res)
								console.log(res.data.msg)
								uni.$emit('updatelist', 10)
								this.$refs.uToast.show({
									title: res.data.msg,
									type: 'warning',
									url: '/pages/index/listCard/home'
								}, 300)
							})			
					}
				}
			});
		},
		getDate(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();
			if (type === 'start') {
				year = year - 60;
			} else if (type === 'end') {
				year = year + 2;
			}
			month = month > 9 ? month : '0' + month;;
			day = day > 9 ? day : '0' + day;
			return `${year}-${month}-${day}`;
		},
		navigationToEvent() {
			console.log(this.detailmsg.fourArea, 'this.detailList.fourArea')
			console.log('this.checkop', this.checkop)
			if (this.checkop == 1) {
				uni.navigateTo({
					// url: '/pages/drawmap/index?&lat=' + this.detailmsg.lat + '&lng=' + this.detailmsg.lng + 
					url: `/pages/drawmap/index?&lat=${this.detailmsg.lat}&lng=${this.detailmsg.lng}`
					// url: '/pages/drawmap/index'
				})
			} else {
				uni.navigateTo({
					url: `/pages/drawmap/index?&lat=${this.detailmsg.lat}&lng=${this.detailmsg.lng}&fourArea=${this.detailmsg.fourArea}&type=check`
					// url: '/pages/drawmap/index?&lat=' + this.detailmsg.lat + '&lng=' + this.detailmsg.lng + '&type=check'
					// url: '/pages/drawmap/index'
				})
			}
		},
		//返回上一级
		goBack() {
			uni.navigateBack()
		},
		//获取当前位置
		getLocation() {
			uni.getLocation({
				type: 'wgs84',
				geocode: true,
				success: res => {
					console.log(res, 888)
					this.form.lat = res.latitude
					this.form.lng = res.longitude
					this.getaddress(this.form.lng, this.form.lat)
				}
			});
		},
		//重新定位
		rePostioning() {
			uni.showLoading({
				title: '正在获取当前位置'
			});
			setTimeout(() => {
				this.getLocation()
			}, 800)
		},

		uperr(a, b, c) {
			console.log(a, b, c)
		},
		getaddress(lat, lon) {
			this.test.getRegeo({
				location: lat + ',' + lon, //location的格式为'经度,纬度'
				//成功回调
				success: (data) => {
					console.log(data[0].regeocodeData.formatted_address, data[0].name)
					this.form.address = data[0].regeocodeData.formatted_address
				},
				fail: function(info) {
					//失败回调
					console.log(info)
				}
			})
		},
	}
}
